package cn.tedu.boot34.controller;

import cn.tedu.boot34.eneity.Emp;
import cn.tedu.boot34.mapper.EmpMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class EmpController {

    @Autowired
    EmpMapper mapper;

    @RequestMapping("/insert")
    public String insert(Emp emp) {

        System.out.println("emp = " + emp);
        mapper.insert(emp);
        return "添加完毕!";

    }

    @RequestMapping("/select")
    public String select() {

        List<Emp> list = mapper.select();
        String html = "<table border=1>";
        html += "<caption>员工列表</caption>";
        html += "<tr><th>id</th><th>名字</th><th>工资(时薪)</th><th>工作</th><th>操作</th></tr>";
        //把查询出来的员工信息添加到表格中
        for (Emp e : list
        ) {
            html += "<tr>";
            html += "<td>" + e.getId() + "</td>";
            html += "<td>" + e.getName() + "</td>";
            html += "<td>" + e.getSal() + "</td>";
            html += "<td>" + e.getJob() + "</td>";
            html += "<td><a href='/delete?id=" + e.getId() + "'>删除</a></td>";
            html += "</tr>";
        }

        html += "</table>";
        return html;
    }

    @RequestMapping("/update")
    public String update(Emp emp) {
        mapper.update(emp);
        return "修改完毕!";
    }

    @RequestMapping("/delete")
    public String delete(int id) {
        mapper.deleteById(id);
        return "修改完毕!<a href='select'>返回列表页面</a>";
    }
}
